<template>
  <div class="login">
    <my-header>
      <span>登录</span>
    </my-header>
    <div class="go">
      <router-link to="/home">随便逛逛</router-link>
    </div>
    <div class="login-box">
      <div class="user-img">
        <img src="../assets/logo.png" alt />
      </div>
      <input type="text" v-model="v1" @keydown.enter="login" placeholder="请输入手机号或用户名" />
      <input type="password" v-model="v2" @keydown.enter="login" placeholder="请输入密码" />
      <button @click="login">登录</button>
      <router-link to="/register">
        <p>注册</p>
      </router-link>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'
import MyHeader from "@/components/MyHeader.vue";
import {baseURL} from '../http/require.js'

import axios from "axios";

// 局部
import Vue from "vue";
import { Dialog } from "vant";
// 全局注册
Vue.use(Dialog);

export default {
  name: "HomeView",
  data() {
    return {
      v1: "",
      v2: ""
    };
  },
  components: {
    MyHeader,
    [Dialog.Component.name]: Dialog.Component
  },
  methods: {
    login() {
      if (this.v1.length != 0 && this.v2.length != 0) {
        axios
          .post(
            `${baseURL}/user/login`,
            { userName: this.v1, phone: this.v1, passWord: this.v2 },
            {
              headers: "application/x-www-form-urlencoded"
            }
          )
          .then(response => {
            let data = response.data;
            // console.log(data);
            if (data.code == 200) {
              // console.log(data);
              let { userName, phone, token, id,balance,touxiang } = data;
              // console.log(touxiang);
              localStorage.setItem("token", token);
              this.$store.dispatch("user/setPhoneAsync", phone);
              this.$store.dispatch("user/setUserNameAsync", userName);
              this.$store.dispatch("user/setIdAsync", id);
              this.$store.dispatch("user/setBalanceAsync", balance);
              this.$store.dispatch("user/setTouXiangAsync", touxiang);

              Dialog.alert({
                message: data.msg
              }).then(() => {
                setTimeout(() => {
                  this.$router.push("/home");
                }, 0);
              });
            } else {
              Dialog.alert({
                message: data.msg
              }).then(() => {
                return;
              });
            }
          });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../css/reset.css";
.login {
  width: 100%;
  .go {
    width: 100%;
    height: 50px;
    // background-color: red;
    line-height: 50px;
    font-size: 18px;
    display: flex;
    justify-content: flex-end;
    padding-right: 20px;
    box-sizing: border-box;
    a {
      color: #ccc;
    }
  }
  .login-box {
    width: 100%;
    height: 565px;
    // background-color: red;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .user-img {
      width: 100px;
      height: 100px;
      border-radius: 20px;
      margin-bottom: 20px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    input {
      width: 80%;
      height: 50px;
      font-size: 18px;
      padding-left: 10px;
      border-radius: 10px;
      border: 2px solid #faca2a;
      outline: none;
      margin-bottom: 20px;
    }
    button {
      width: 80%;
      height: 60px;
      font-size: 24px;
      color: #fff;
      background-color: #faca2a;
      border-radius: 10px;
      border: 0;
    }
    p {
      font-size: 17px;
      font-weight: 600;
      margin-top: 10px;
      color: #000;
    }
  }
}
</style>
